/*
 * Created on Jul 11, 2004
 */
package dk.aiszone.lang.autosource.generatorobjects;

/**
 * Enum class containing the valid scopes of Java<sup>TM</sup>
 * 
 * @author <a href="mailto:ais@mail.dk">Ais</a>
 * @since 1.0
 * @version 1.0
 */
public class Scope implements Comparable<Scope> {

    private static final int _DEFAULT_SCOPE = 3;
    private static final int _PRIVATE_SCOPE = 4;
    private static final int _PROTECTED_SCOPE = 2;
    private static final int _PUBLIC_SCOPE = 1;

    /** The default package protected scope */
    public static final Scope DEFAULT = new Scope(_DEFAULT_SCOPE);

    /** The private scope */
    public static final Scope PRIVATE = new Scope(_PRIVATE_SCOPE);

    /** The procted scope */
    public static final Scope PROTECTED = new Scope(_PROTECTED_SCOPE);

    /** The public scope */
    public static final Scope PUBLIC = new Scope(_PUBLIC_SCOPE);

    /**
     * Converts the supplied <code>String</code> to a <code>Scope</code> entity.<br>
     * The method supports the following texts:
     * <ul>
     * <li>&quot;public&quot; -> <code>Scope.PUBLIC</code>.
     * <li>&quot;protected&quot; -> <code>Scope.PROTECTED</code>.
     * <li>&quot;private&quot; -> <code>Scope.PRIVATE</code>.
     * </ul>
     * All other <code>Strings</code> will result in <code>Scope.DEFAULT</code>.
     * 
     * @param scopeText
     *            The <code>String</code> to convert.
     * @return The resulting <code>Scope</code>
     */
    public static Scope toScope(String scopeText) {
        if ("public".equals(scopeText)) {
            return PUBLIC;
        } else if ("protected".equals(scopeText)) {
            return PROTECTED;
        } else if ("private".equals(scopeText)) {
            return PRIVATE;
        } else {
            return DEFAULT;
        }
    }

    private int code;

    private Scope(int code) {
        this.code = code;
    }

    public int compareTo(Scope other) {
        if (code == other.code) {
            return 0;
        } else if (code > other.code) {
            return 1;
        }
        return -1;
    }

    @Override public String toString() {
        switch (code) {
            case _PUBLIC_SCOPE:
                return "public ";
            case _PROTECTED_SCOPE:
                return "protected ";
            case _DEFAULT_SCOPE:
                return "/* pp */ ";
            case _PRIVATE_SCOPE:
                return "private ";
            default:
                return "Unknow scope";
        }
    }
}